﻿/*
COPYRIGHT (C) 2010 EPISERVER AB

THIS FILE IS PART OF PROPERTY PACK FOR EPISERVER CMS.
DYNAMIC CONTENT CONTRIB FOR EPISERVER CMS IS FREE SOFTWARE: YOU CAN REDISTRIBUTE IT AND/OR MODIFY IT UNDER THE TERMS OF THE GNU LESSER GENERAL PUBLIC LICENSE VERSION v2.1 AS PUBLISHED BY THE FREE SOFTWARE FOUNDATION.

PROPERTY PACK FOR EPISERVER CMS IS DISTRIBUTED IN THE HOPE THAT IT WILL BE USEFUL, BUT WITHOUT ANY WARRANTY; WITHOUT EVEN THE IMPLIED WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE. SEE THE GNU LESSER GENERAL PUBLIC LICENSE FOR MORE DETAILS.
YOU SHOULD HAVE RECEIVED A COPY OF THE GNU LESSER GENERAL PUBLIC LICENSE ALONG WITH PROPERTY PACK FOR EPISERVER CMS. IF NOT, SEE <HTTP://WWW.GNU.ORG/LICENSES/>.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EPiServer.SpecializedProperties;
using EPiServer.Core;
using EPiServer.PlugIn;
using System.Collections;
using System.Xml.Serialization;
using System.IO;
using System.Xml.Linq;
using EPiServer.DataAbstraction;

namespace EPiServer.Research.Properties.PropertyList
{
    /// <summary>
    /// Holds a list of properties
    /// </summary>
    [Serializable]
    [PageDefinitionTypePlugIn(DisplayName = "Property List")]
    public class PropertyList : PropertyXhtmlString, IEnumerable<PropertyData>, IEnumerable
    {

        public static string SerializeToXmlString<T>(T o)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            StringWriter writer = new StringWriter();
            serializer.Serialize(writer, o);
            writer.Close();
            return writer.GetStringBuilder().ToString();
        }

        public static T Deserialize<T>(string s)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            StringReader sr = new StringReader(s);
            T obj = (T) serializer.Deserialize(sr);
            sr.Close();
            return obj;
        }

        public PropertyData CreateNewPropertyData()
        {
            var sett = GetSetting(typeof(PropertyListSettings)) as PropertyListSettings;
            var newPData = Activator.CreateInstance(PageDefinitionType.Load(sett.PropertyId).DefinitionType) as PropertyData;
            return newPData;
        }


        public List<PropertyData> PropertyCollection
        {
            get
            {
                //Deserialize from value
                if (string.IsNullOrEmpty((string)Value)) return new List<PropertyData>();

                var lst = new List<PropertyData>();
                XElement root = XElement.Parse((string)Value);
                foreach (XElement xe in root.Elements("property"))
                {
                    var pd = CreateNewPropertyData();
                    pd.ParseToSelf(xe.Value);
                    lst.Add(pd);
                }
                return lst;
            }
            set
            {
                XElement root = new XElement("properties");
                foreach (PropertyData pd in value)
                {
                    root.Add(new XElement("property") { Value = pd.ToString() });
                }
                //serialize to value
                Value = root.ToString();
            }
        }

        #region Constructors
        public PropertyList()
        {
            //PropertyCollection = new List<PropertyData>();
        }
        public PropertyList(IEnumerable<PropertyData> List)
        {
            PropertyCollection = List.ToList();
        }
        public PropertyList(params PropertyData[] List)
        {
            PropertyCollection = List.ToList();
        }
        #endregion

        public override IPropertyControl CreatePropertyControl()
        {
            return new PropertyListControl();
        }


        #region IEnumerable<PropertyData> Members

        public IEnumerator<PropertyData> GetEnumerator()
        {
            return PropertyCollection.GetEnumerator();
        }

        #endregion

        #region IEnumerable Members

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return PropertyCollection.GetEnumerator();
        }

        #endregion
    }
}
